﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using MeizuSDK.Presentation;
using System.Drawing;
using MeizuSDK.Core;
using System.Threading;
using MeizuSDK.Drawing;
using System.IO;

namespace Demo
{
    class MainWindow : Form
    {
        const int DefaultButtonWidth_V = 220;
        const int DefaultButtonWidth_H = 340;

        private static readonly string BgImagePath_V = Path.Combine(Application.StartupPath, "Images\\Background_V.png");
        private static readonly string BgImagePath_H = Path.Combine(Application.StartupPath, "Images\\Background_H.png");

        ToolBar toolbar = new ToolBar();
        ScrollableControl scrollPanel = new ScrollableControl();

        Button basicControlButton = new Button();
        Button listControlButton = new Button();
        Button telephonyButton = new Button();
        Button gravityButton = new Button();
        Button pictureButton = new Button();
        Button irSensorButton = new Button();
        Button popupDialogButton = new Button();
        Button shellNotificationButton = new Button();
        Button mapButton = new Button();

        Button touchButton = new Button();
        Button gridMenuButton = new Button();
        Button buttonBarButton = new Button();
        Button progressButton = new Button();
        Button eglButton = new Button();
        Button scrollableWinButton = new Button();
        Button COMButton = new Button();
        Button toastButton = new Button();
        Button winformButton = new Button();

        public MainWindow()
        {
            UsbConnection.StatusChanged += new EventHandler<UsbConnectionEventArgs>(UsbConnection_StatusChanged);

            Text = "MainWindow";
            AnimationIn = AnimationType.ZoomIn;
        }
        /// <summary>
        /// 初始化控件
        /// </summary>
        protected override void InitializeComponents()
        {
            toolbar.SetTextButton(ToolBarButtonIndex.LeftTextButton, true, true, "退出");
            toolbar.SetTextButton(ToolBarButtonIndex.MiddleTextButton, true, true, "显示对话框");
            toolbar.SetTextButton(ToolBarButtonIndex.RightTextButton, true, true, "序列号");
            toolbar.ButtonClick += new EventHandler<ToolBar.ButtonEventArgs>(toolbar_ButtonClick);
            Controls.Add(toolbar);


            Controls.Add(scrollPanel);

            basicControlButton.Text = "基本控件";
            basicControlButton.Click += new EventHandler(basicControlButton_Click);

            listControlButton.Text = "列表控件";
            listControlButton.Click += new EventHandler(listControlButton_Click);

            telephonyButton.Text = "电话";
            telephonyButton.Click += new EventHandler(telephonyButton_Click);

            gravityButton.Text = "重力感应";
            gravityButton.Click += new EventHandler(gravityButton_Click);

            pictureButton.Text = "图片";
            pictureButton.Click += new EventHandler(pictureButton_Click);

            irSensorButton.Text = "红外探测";
            irSensorButton.Click += new EventHandler(irSensorButton_Click);

            popupDialogButton.Text = "弹出式对话框";
            popupDialogButton.Click += new EventHandler(popupDialogButton_Click);

            shellNotificationButton.Text = "Shell通知消息";
            shellNotificationButton.Click += new EventHandler(shellNotificationButton_Click);

            mapButton.Text = "GoogleMaps";
            mapButton.Click += new EventHandler(mapButton_Click);


            touchButton.Text = "多点触摸";
            touchButton.Click += new EventHandler(touchButton_Click);

            gridMenuButton.Text = "Grid Menu";
            gridMenuButton.Click += new EventHandler(gridMenuButton_Click);

            buttonBarButton.Text = "ButtonBar";
            buttonBarButton.Click += new EventHandler(buttonBarButton_Click);

            progressButton.Text = "Popup Progress";
            progressButton.Click += new EventHandler(progressButton_Click);

            scrollableWinButton.Text = "ScrollWin";
            scrollableWinButton.Click += new EventHandler(scrollableWinButton_Click);

            COMButton.Text = "COM展示";
            COMButton.Click += new EventHandler(COMButton_Click);

            toastButton.Text = "Toast演示";
            toastButton.Click += new EventHandler(toastButton_Click);

            winformButton.Text = "WinForm控件";
            winformButton.Click += new EventHandler(winformButton_Click);

            eglButton.Text = "OpenGL";
            eglButton.Click += new EventHandler(eglButton_Click);

            scrollPanel.Controls.Add(basicControlButton);
            scrollPanel.Controls.Add(listControlButton);
            scrollPanel.Controls.Add(telephonyButton);
            scrollPanel.Controls.Add(gravityButton);
            scrollPanel.Controls.Add(pictureButton);
            scrollPanel.Controls.Add(irSensorButton);
            scrollPanel.Controls.Add(popupDialogButton);
            scrollPanel.Controls.Add(shellNotificationButton);
            scrollPanel.Controls.Add(mapButton);


            scrollPanel.Controls.Add(touchButton);
            scrollPanel.Controls.Add(gridMenuButton);
            scrollPanel.Controls.Add(buttonBarButton);
            scrollPanel.Controls.Add(progressButton);
            scrollPanel.Controls.Add(eglButton); // OpenGL ES 实例窗口按钮
            scrollPanel.Controls.Add(scrollableWinButton);
            scrollPanel.Controls.Add(COMButton);
            scrollPanel.Controls.Add(toastButton);
            scrollPanel.Controls.Add(winformButton);

            // 默认布局
            RelayoutControls(ScreenRotationAngle.Angle_0);
        }
        /// <summary>
        /// 重新布局控件
        /// </summary>
        /// <param name="orientation"></param>
        protected override void RelayoutControls(ScreenRotationAngle orientation)
        {
            bool isVerticle = (orientation == ScreenRotationAngle.Angle_0 || orientation == ScreenRotationAngle.Angle_180);

            // 设置主窗口背景
            string bgImgPath = isVerticle ? BgImagePath_V : BgImagePath_H;
            if (File.Exists(bgImgPath))
            {
                BackgroundImage = new Bitmap(bgImgPath);
            }

            toolbar.Size = new Size(Width, ToolBar.HEIGHT);
            toolbar.Location = new Point(0, Height - ToolBar.HEIGHT);

            scrollPanel.Size = new Size(Width, Height - ToolBar.HEIGHT);
            scrollPanel.Location = new Point(0, 0);

            basicControlButton.Location = new Point(10, 10);

            listControlButton.Location = new Point(10, 110);

            telephonyButton.Location = new Point(10, 210);

            gravityButton.Location = new Point(10, 310);

            pictureButton.Location = new Point(10, 410);

            irSensorButton.Location = new Point(10, 510);

            popupDialogButton.Location = new Point(10, 610);

            shellNotificationButton.Location = new Point(10, 710);

            mapButton.Location = new Point(10, 810);



            touchButton.Location = new Point(isVerticle ? 250 : 350, 10);

            gridMenuButton.Location = new Point(isVerticle ? 250 : 350, 110);

            buttonBarButton.Location = new Point(isVerticle ? 250 : 350, 210);

            progressButton.Location = new Point(isVerticle ? 250 : 350, 310);

            scrollableWinButton.Location = new Point(isVerticle ? 250 : 350, 410);

            COMButton.Location = new Point(isVerticle ? 250 : 350, 510);

            toastButton.Location = new Point(isVerticle ? 250 : 350, 610);

            winformButton.Location = new Point(isVerticle ? 250 : 350, 710);

            eglButton.Location = new Point(isVerticle ? 250 : 350, 810);

            switch (orientation)
            {
                case ScreenRotationAngle.Angle_90:
                case ScreenRotationAngle.Angle_270:
                    {
                        foreach (Control ctr in scrollPanel.Controls)
                        {
                            if (ctr is Button)
                            {
                                ctr.Width = DefaultButtonWidth_H;
                            }
                        }
                    }
                    break;
                default:
                    {
                        foreach (Control ctr in scrollPanel.Controls)
                        {
                            if (ctr is Button)
                            {
                                ctr.Width = DefaultButtonWidth_V;
                            }
                        }
                    }
                    break;
            }
        }

        void mapButton_Click(object sender, EventArgs e)
        {
            StaticMapWindow win = new StaticMapWindow();
            win.ShowDialog(this);
        }

        void winformButton_Click(object sender, EventArgs e)
        {
            WinFormControlDemoWindow win = new WinFormControlDemoWindow();
            win.ShowDialog(this);
        }

        void shellNotificationButton_Click(object sender, EventArgs e)
        {
            ShellNotificationWindow win = new ShellNotificationWindow();
            win.ShowDialog(this);
        }

        void toastButton_Click(object sender, EventArgs e)
        {
            Toast.Show(this, "啊哈！Toast！", Toast.LongDuration);
        }

        void popupDialogButton_Click(object sender, EventArgs e)
        {
            ListBox box = new ListBox();
            box.Size = new Size(400, 500);
            box.ForeColor = Color.White;

            for (int i = 0; i < 50; i++)
            {
                ListItem item = new ListItem("俺是列表项_" + (i + 1).ToString(), null);
                box.Items.Add(item);
            }

            PopupDialog pd = new PopupDialog("俺是对话框！", box);

            PopupDialogButton btn1 = new PopupDialogButton("关闭");
            pd.Buttons.Add(btn1);
            PopupDialogButton btn2 = new PopupDialogButton("取消");
            btn2.Click += new EventHandler(btn2_Click);
            pd.Buttons.Add(btn2);
            PopupDialogButton btn3 = new PopupDialogButton("不知道干啥");
            pd.Buttons.Add(btn3);

            pd.ShowDialog();
        }

        void btn2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("你点击了“取消”按钮！");
        }

        void UsbConnection_StatusChanged(object sender, UsbConnectionEventArgs e)
        {
            if (e.Status == UsbConnectionStatus.MassStorage)
            {
                MessageBox.Show("USB: " + e.Status.ToString() + ", Exit");
                AnimationOut = AnimationType.ZoomOut;
                Application.Exit();
            }
            else
            {
                MessageBox.Show("USB: " + e.Status.ToString());
            }
        }

        void toolbar_ButtonClick(object sender, ToolBar.ButtonEventArgs e)
        {
            switch (e.Index)
            {
                case ToolBarButtonIndex.LeftTextButton:
                    if (MessageBox.DialogResult.OK == MessageBox.Show("是否要退出?", "警告", MessageBox.MessageBoxButtons.MZ_OKCANCEL, MessageBox.HomeKeyReturnValue.SHK_RET_DEFAULT))
                    {
                        AnimationOut = AnimationType.ZoomOut;
                        Application.Exit();
                    }
                    break;
                case ToolBarButtonIndex.MiddleTextButton:
                    {
                        ListWindow win = new ListWindow();
                        if (win.ShowDialog(this) == DialogResult.Yes)
                        {
                            MessageBox.Show("DialogResult=Yes", "提示");
                        }

                    }
                    break;
                case ToolBarButtonIndex.RightTextButton:
                    MessageBox.Show("IMEI序列号" + DeviceEnv.IMEISerialNumber + "\n设备序列号" + DeviceEnv.DeviceSerialNumber);
                    break;
            }
        }

        void listControlButton_Click(object sender, EventArgs e)
        {
            ListWindow win = new ListWindow();
            win.ShowDialog(this);
        }

        void basicControlButton_Click(object sender, EventArgs e)
        {
            WaitDialog.Begin(this);
            Thread.Sleep(1000);
            WaitDialog.End();

            BasicControlWindow win = new BasicControlWindow();
            win.ShowDialog(this);
        }

        void telephonyButton_Click(object sender, EventArgs e)
        {
            PhoneWindow win = new PhoneWindow();
            win.ShowDialog(this);
        }

        void gravityButton_Click(object sender, EventArgs e)
        {
            GravitySensorWindow win = new GravitySensorWindow();
            win.ShowDialog(this);
        }

        void pictureButton_Click(object sender, EventArgs e)
        {
            PictureWindow win = new PictureWindow();
            win.ShowDialog(this);
        }

        void irSensorButton_Click(object sender, EventArgs e)
        {
            IRSensorWindow win = new IRSensorWindow();
            win.ShowDialog(this);
        }

        void touchButton_Click(object sender, EventArgs e)
        {
            MultiTouchWindow win = new MultiTouchWindow();
            win.ShowDialog(this);
        }

        void gridMenuButton_Click(object sender, EventArgs e)
        {
            GridMenuWindow win = new GridMenuWindow();
            win.ShowDialog(this);
        }

        void buttonBarButton_Click(object sender, EventArgs e)
        {
            ButtonBarWindowWindow win = new ButtonBarWindowWindow();
            win.ShowDialog(this);
        }


        void progressButton_Click(object sender, EventArgs e)
        {
            PopupProgressWindow win = new PopupProgressWindow();
            win.ShowDialog(this);
        }

        void eglButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this, "未完成", "提示");
            //DemoEglWindow win = new DemoEglWindow();
            //win.ShowDialog(this);
        }

        void scrollableWinButton_Click(object sender, EventArgs e)
        {
            ScrollableWindow win = new ScrollableWindow();
            win.ShowDialog(this);
        }

        void COMButton_Click(object sender, EventArgs e)
        {
            //IconToolBarWindow win = new IconToolBarWindow();
            //win.ShowDialog(this);

            COMWindow win = new COMWindow();
            win.ShowDialog(this);
        }
    }
}
